//
//  ProjectViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/19.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//


#import "ProjectViewController.h"
#import "ProjectHeaderView.h"
#import "FTHomeTableViewCell.h"
#import "ResponseManager.h"
#import "DetailViewController.h"
#import "SearchViewController.h"

@interface ProjectViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic ,strong)NSMutableArray *dataSource;

@end

@implementation ProjectViewController

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupCenterItemWithText:nil action:@selector(pusNextView)];
    [self setupSubView];
    [self.view addSubview:self.tableView];
    UINib *nib = [UINib nibWithNibName:@"FTHomeTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kHomeTableViewCell];
    [self getWishNetwork];

}

- (void)pusNextView {
    
    SearchViewController *sarchVC = [[SearchViewController alloc]init];
    [self.navigationController pushViewController:sarchVC animated:YES];
    
}

-(void)getWishNetwork{
    __weak typeof(self) myself = self;
    [ResponseManager startGetRequestWithURLString:kGetWishListURL parameters:nil completionHandler:^(FBResponse *response) {
//        stop1 = true;
        if (response.success) {
            PLLOG(@"%@",response.resultValue);
//            [myself checkRequestFinished];
            NSArray *arr = [WishesModel modelArrayWithDictionaryArr:response.resultValue];
            myself.dataSource = [arr mutableCopy];
            //            [myself.tableView reloadData];
            [myself.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        }else{
            [self addAlertControllerWithTitle:@"提示" message:@"请求失败" actionNum:2 style:UIAlertControllerStyleAlert handler:^(UIAlertAction *action) {
                [myself getWishNetwork];
            }];
        }
    }];
}

- (void)setupSubView {
    ProjectHeaderView *headerView = [ProjectHeaderView loadMyHeaderView];
    headerView.ownerVC = self;
    headerView.frame = CGRectMake(0, 0, kScreenWidth, 300 * kScreenHeightScale );
    //    headerView.delegate = self;
    self.tableView.tableHeaderView = headerView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80 * kScreenHeightScale;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FTHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHomeTableViewCell forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *vc = [[DetailViewController alloc]init];
    vc.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = RGBCOLOR(239.0f, 239.0f, 244.0f, 1);
    }
    return _tableView;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

@end
